﻿using DDD.Application;
using GoodsModule.Domain;
using Microsoft.EntityFrameworkCore;

namespace GoodsModule.Application.Queries
{
    public record GoodsGetListReq(int? goodsCategoryId, GoodsStatus? status) : IQuery<List<Domain.GoodsAggregate.Goods>>;

    public class GoodsGetListHandler(Infrastructure.Queries.GoodsQuery goodsQuery)
        : IQueryHandler<GoodsGetListReq, List<Domain.GoodsAggregate.Goods>>
    {
        public Task<List<Domain.GoodsAggregate.Goods>> Handle(GoodsGetListReq request, CancellationToken cancellationToken)
        {
            var q = goodsQuery.AsQueryable();
            if (request.status != null)
            {
                q = q.Where(e => e.Status == request.status);
            }
            if (request.goodsCategoryId != null)
            {
                q = q.Where(e => e.GoodsCategoryId == request.goodsCategoryId);
            }
            return q.ToListAsync(cancellationToken);
        }
    }
}
